<?php

namespace app\admin\controller;

use addon\platformcoupon\model\PlatformcouponType;
use addon\supermember\model\Config;
use addon\supermember\model\MemberLevelOrder;
use app\model\member\MemberLevel;

class Membercard extends BaseAdmin
{
    /**
     * 会员卡订单
     * @return mixed
     */
    public function order()
    {
        if (request()->isAjax()) {
            $page        = input('page', 1);
            $page_size   = input('page_size', PAGE_LIST_ROWS);
            $status = input('status', '');//订单状态
            $order_no = input('order_no', '');
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            $nickname = input('nickname', '');
            $mobile = input('mobile', '');
            $alias = 'mo';
            $join = [
                [ 'member m', 'mo.buyer_id = m.member_id', 'inner' ]
            ];
            $condition   = [

            ];
            if ($status !== '') {
                $condition[] = ['mo.order_status', '=', $status];
            }
            if ($order_no) {
                $condition[] = ['mo.order_no', '=', $order_no];
            }
            if ($nickname) {
                $condition[] = ['m.nickname', '=', $nickname];
            }
            if ($mobile) {
                $condition[] = ['m.mobile', '=', $mobile];
            }
            if ($start_time && !$end_time) {
                $condition[] = ['mo.create_time', '>=', date_to_time($start_time)];
            } elseif (!$start_time && $end_time) {
                $condition[] = ['mo.create_time', '<=', date_to_time($end_time)];
            } elseif ($start_time && $end_time) {
                $condition[] = ['mo.create_time', 'between', [date_to_time($start_time), date_to_time($end_time)]];
            }
            $order = 'mo.create_time desc';
            $field = 'mo.*,m.mobile,m.nickname,m.member_id';
            $level_order = new MemberLevelOrder();
            $data = $level_order->getLevelOrderPageList($condition, $page, $page_size,$field,$order,$alias,$join);
            $list = $data['data']['list'];
            foreach ($list as $k=>$v){

                $fenxiao = model('fenxiao')->getInfo([['member_id','=',$v['member_id']]],'parent');
                if ($fenxiao['parent']){
                    $parent_fenxiao = model('fenxiao')->getInfo([['fenxiao_id','=',$fenxiao['parent']]],'member_id');
                    $member = model('member')->getInfo([['member_id','=',$parent_fenxiao['member_id']]],'nickname,invitation_code');
                    $list[$k]['recommender'] = $member?$member['nickname']:'';
                    $list[$k]['recommend_no'] = $member?$member['invitation_code']:'';
                } else {
                    $list[$k]['recommender'] = '';
                    $list[$k]['recommend_no'] = '';
                }

            }
            $data['data']['list'] = $list;

            return $data;
        } else {
            $order = new MemberLevelOrder();
            $total_level_num = $order->getOrderCount([  ['order_status', '=', MemberLevelOrder::ORDER_PAY] ])['data'];
            $total_money = $order->getOrderSum([  ['order_status', '=', MemberLevelOrder::ORDER_PAY] ], 'order_money')['data'];
            $this->assign('total_level_num', $total_level_num);
            $this->assign('total_money', $total_money);
            return $this->fetch('membercard/order');
        }
    }

    /**
     * 开卡协议
     * @return mixed
     */
    public function agreement(){
        if (request()->isAjax()) {
            //设置注销协议
            $title = input('title', '');
            $content = input('content', '');
            $config_model = new Config();
            return $config_model->setMemberCardDocument($title, $content, 0, 'admin');
        } else {
            //获取注销协议
            $config_model = new Config();
            $document_info = $config_model->getMemberCardDocument(0, 'admin');
            $this->assign('document_info', $document_info);
            return $this->fetch('membercard/agreement');
        }
    }


    public function edit()
    {
        $member_level_model = new MemberLevel();
        if (request()->isAjax()) {
            $data = [
                'remark'           => input('remark', ''),
                'first_content_description'           => input('first_content_description', ''),
                'second_content_description'           => input('second_content_description', ''),
                'first_remark'           => input('first_remark', ''),
                'second_remark'           => input('second_remark', ''),
            ];
            $level_id = input('level_id', 0);

            return $member_level_model->editMemberLevel($data, [['level_id', '=', $level_id]]);
        } else {
            $level_id   = input('id', 5);
            $level_info = $member_level_model->getMemberLevelInfo([['level_id', '=', $level_id]]);


            $this->assign('level_info', $level_info['data']);
            //if (empty($level_info['data'])) return $this->error('未获取到会员卡数据', addon_url('supermember://admin/membercard/lists'));
            $this->assign('level_time', $member_level_model->level_time);

            return $this->fetch('membercard/edit');
        }
    }

    public function editset()
    {
        $member_level_model = new MemberLevel();
        if (request()->isAjax()) {
            $data = [
                'level_price'=>input('level_price', ''),
                'consume_discount'           => input('consume_discount', ''),
                'perosn_consume_discount'           => input('perosn_consume_discount', ''),
                'staff_consume_discount'           => input('staff_consume_discount', ''),
            ];
            $level_id = input('level_id', 0);

            return $member_level_model->editMemberLevel($data, [['level_id', '=', $level_id]]);
        } else {
            $level_id   = input('id', 5);
            $level_info = $member_level_model->getMemberLevelInfo([['level_id', '=', $level_id]]);


            $this->assign('level_info', $level_info['data']);
            //if (empty($level_info['data'])) return $this->error('未获取到会员卡数据', addon_url('supermember://admin/membercard/lists'));
            $this->assign('level_time', $member_level_model->level_time);

            return $this->fetch('membercard/editset');
        }
    }
}